package com.bk.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.moomi.common.model.BkItem;
import com.moomi.common.model.BkItemSelection;
import com.moomi.common.model.BkSelection;

/**
 * 选项管理
 * 
 * @author cen
 *
 */
public class BKSelectionController extends BKAuthController {

	@Override
	public void index() {
		super.index();
		setAttr("title", "服务详情管理");
		// List<BkSelection> items = BkSelection.dao.search("shopId",
		// getShopId());
		List<BkSelection> items = BkSelection.dao.find(Db.getSql("AdminSelections"), getShopId());
		setAttr("items", items);
		renderJson(items);
		setAttr("items", items);
		render("table.html");
	}

	@Override
	public void update() {
		super.update();
		BkSelection model = (BkSelection) updateModel(BkSelection.class);
	}

	@Override
	public void info() {
		setTitle("服务详情");
		super.info();
		modelInfo(BkSelection.class, "form.html", false);
	}

	@Override
	public void delete() {
		super.delete();
		Integer id = getParaToInt("id");
		BkSelection selection = BkSelection.dao.findById(id);
		selection.setFlag(1);
		boolean flag = selection.update();
		if (flag) {
			renderSuccess();
		} else {
			renderMsg("删除失败");
		}
		// deleteModel(BkSelection.class);
	}

	public void select() {
		setAttr("action", "updateSelection");
		// setAttr("list", PxlmTeacher.dao.search("schoolId", getSchoolId()));
		int id = getId();
		// BkItem item = BkItem.dao.findById(id);
		BkSelection selection = BkSelection.dao.findById(id);

		List<BkItem> AllItems = BkItem.dao.search("shopId", getShopId());

		List<BkItemSelection> itemSelections = BkItemSelection.dao.search("selectionId", id);
		ArrayList<HashMap<String, Object>> list = new ArrayList<>();
		for (BkItem item : AllItems) {
			HashMap<String, Object> map = new HashMap<>();
			map.put("checked", "");
			map.put("info", item);
			for (BkItemSelection itemSelection : itemSelections) {
				if (itemSelection.getItemId() == item.getId()) {
					map.put("checked", "checked");
				}
			}
			list.add(map);
		}
		setAttr("teachers", list);
		setAttr("info", selection);
		setAttr("title", selection.getName());
		render("selection.html");
	}

	public void updateSelection() {
		getFiles();
		int selectionId = getId();
		List<BkItemSelection> itemSelections = BkItemSelection.dao.search("selectionId", selectionId);
		for (BkItemSelection bkItemSelection : itemSelections) {
			bkItemSelection.delete();
		}

		try {
			String[] values = getParaValues("selected");
			for (String string : values) {
				BkItemSelection itemSelection = new BkItemSelection();
				itemSelection.setItemId(Integer.parseInt(string));
				itemSelection.setSelectionId(selectionId);
				boolean flag = itemSelection.save();
				System.out.println(string + " " + flag);
			}
		} catch (Exception e) {
		}

		redirect("index");

	}

	public void online() {
		int selectionId = getId();
		BkSelection selection = BkSelection.dao.findById(selectionId);
		selection.updateOnlineStatus();
		renderSuccess();
	}
}
